package com.ittx.dzt.jinglyun.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.SkuQueryDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.jinglyun.handler.AbstractBaseJinglYunApiHandler;
import com.ittx.dzt.sys.response.Result;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据spuCode 获取商品详情
 * <p>
 * cy.sku.download.bySpuCode
 * /open/api/item/listItems 调用该接口分页查询平台商品信息
 * https://jlopen.jinglyun.com/main/doc?docType=2&cateId=10001&docId=20013
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 */
@Slf4j
@Service
public class JinglYunSkuDownloadBySpuCodeService extends AbstractBaseJinglYunApiHandler<SkuQueryDTO> {
	@Override
	public String getJinglMethod() {
		return "/open/api/item/listItems";
	}

	@Override
	public String getLocalApiMethod() {
		return PlatformApiConstant.InboundInterface.SKU_DOWNLOAD_BY_PLATFORM_SPU_CODE;
	}

	@Override
	public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, SkuQueryDTO skuQueryDTO) {
		List<Object> list = new ArrayList<>();
		// 平台商品code
		String[] ids = skuQueryDTO.getPlatformSpuCode().split(",");
		for (String id : ids) {
			Result<?> result = this.getSkuById(id, platformApiParamDTO);
			if (result.hasError()) {
				return result;
			}
			Object resultData = result.getData();
			// 同步返回数据
			if (skuQueryDTO.getIsAsync() == 0) {
				list.add(resultData);
			} else {
				platformApiParamDTO.setPlatformIdentifierTag(id);
				result = this.sendRequestToPlatform(platformApiParamDTO, resultData, true);
				if (result.hasError()) {
					return result;
				}
			}
		}
		if (skuQueryDTO.getIsAsync() == 0) {
			return Result.success().addData(list).addMsg(platformApiParamDTO.getStoreConfig().getChainUniqueCode());
		}
		return Result.success();
	}

	/**
	 * 根据id获取商品
	 *
	 * @return
	 */
	public Result<?> getSkuById(String id, PlatformApiParamDTO platformApiParamDTO) {
		try {
			StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
			Map<String, Object> params = new HashMap<>();
			// spuIdList	Array	否	鲸灵内部商品spuId查询条件（Long型数组），给部分需要精确查询场景使用，单次最多10个
			List<String> ids = new ArrayList<>();
			ids.add(id);
			// supplierSpuCodeList	Array	否	供应商商品码查询条件（String型数组），给部分需要精确查询场景使用，单次最多10个
			params.put("supplierSpuCodeList", ids);
			// pageNo	int	否	分页参数。当前页码，默认值： 1
			// pageSize	int	否	分页参数。每页显示数量，默认值： 5，最大值： 30
			Result<?> result = this.executeApi(platformApiParamDTO, params, platformApiParamDTO.getRequestId());
			if (result.hasError()) {
				return result;
			}
			Map<String, Object> resultMap = (Map<String, Object>) result.getData();
			List itemList = MapUtil.get(resultMap, "entry", List.class);
			if (CollUtil.isEmpty(itemList)) {
				String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0018_MSG, platformApiParamDTO.getRequestId(), id);
				return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0018, msg));
			}
			Object product = itemList.get(0);
			if (product == null) {
				String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0018_MSG, platformApiParamDTO.getRequestId(), id);
				return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0018, msg));
			}
			return Result.success().addData(product);
		} catch (Exception e) {
			String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), platformApiParamDTO.getApi(), e.getMessage());
			log.error(msg);
			return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, msg));
		}
	}


}
